<template>
  <div class="comment-post">
    <van-field class="post-field" v-model.trim="message" rows="2" autosize type="textarea" maxlength="50" placeholder="优质评论将会被优先展示" show-word-limit />
    <van-button class="post-btn" @click="onComment" :disabled="message === ''">发布</van-button>
  </div>
</template>

<script>
import { addComment } from '@/api/comment'
import bigInt from 'json-bigint'
export default {
  name: 'CommentPost',
  components: {},
  inject: {
    art_id: {
      type: [Number, String, Object],
      default: null
    }
  },
  props: {
    target: {
      type: [Number, String, Object],
      required: true
    }
  },
  data () {
    return {
      message: ''
    }
  },
  computed: {},
  watch: {},
  created () { },
  mounted () { },
  methods: {
    async onComment () {
      try {
        const { data: res } = await addComment({
          target: bigInt.stringify(this.target),
          content: this.message,
          art_id: this.art_id
        })
        console.log(res)
        this.$emit('comment-success', res.data)
        this.message = ''
        this.$toast.success('发布成功')
      } catch (err) {
        this.$toast.fail('评论失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
.comment-post {
  display: flex;
  padding: 32px 0 32px 32px;
  align-items: center;
  .post-field {
    background: #f5f7f9;
  }
  .post-btn {
    width: 150px;
    border: none;
    padding: 0;
    color: #6ba3d8;
    &::before {
      display: none;
    }
  }
}
</style>
